package com.baway.biliclitent.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.baway.biliclitent.R;
import com.baway.biliclitent.fragment.TomatoType.TomatoDetails;
import com.baway.biliclitent.model.TomatoBean;
import com.baway.biliclitent.view.LiveGridView;
import com.bumptech.glide.Glide;
import com.zhy.android.percent.support.PercentLinearLayout;

import java.util.List;

/**
 * 1.类的用途：番剧界面上方的适配器
 * 2.渠刚
 * 3.@date
 * 4.用途:
 */
public class MyTomAtoCenterAdapter extends RecyclerView.Adapter<MyTomAtoCenterAdapter.MyViewHolder>{
    private Context context;
    List<TomatoBean.ResultBean.PreviousBean.ListBean> octoberList;

    public MyTomAtoCenterAdapter(Context context) {
        this.context = context;
    }
    //设置数据的方法
    public void setData(List<TomatoBean.ResultBean.PreviousBean.ListBean> octoberList){
        this.octoberList = octoberList;

        this.notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.top_recy_item_layout,parent,false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.mImageLz.setImageDrawable(context.getResources().getDrawable(R.drawable.bangumi_home_ic_season_4));
        holder.mTelz.setText("十月新番");
        //将集合传递给GridView设置适配器
        MyBaseAdapter myBaseAdapter = new MyBaseAdapter(context,octoberList);
        holder.myTopGridView.setAdapter(myBaseAdapter);
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        private final ImageView mImageLz;
        private final TextView mTelz;
        private final LiveGridView myTopGridView;

        public MyViewHolder(View itemView) {
            super(itemView);
            mImageLz = (ImageView) itemView.findViewById(R.id.item_image_lz);
            mTelz = (TextView) itemView.findViewById(R.id.item_text_lz);
            myTopGridView = (LiveGridView) itemView.findViewById(R.id.myTopGridView);
        }
    }
    public class MyBaseAdapter extends BaseAdapter{
        private Context context;
        List<TomatoBean.ResultBean.PreviousBean.ListBean> octoberList;

        public MyBaseAdapter(Context context, List<TomatoBean.ResultBean.PreviousBean.ListBean> octoberList) {
            this.context = context;
            this.octoberList = octoberList;
        }

        @Override
        public int getCount() {
            return octoberList==null?0:octoberList.size();
        }

        @Override
        public Object getItem(int i) {
            return octoberList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder = null;
            if(view == null){
                view = LayoutInflater.from(context).inflate(R.layout.tomato_grid_item_layout,null);
                holder = new ViewHolder();
                holder.grid_imageTv = (ImageView) view.findViewById(R.id.grid_imageTv);
                holder.grid_nameTv = (TextView) view.findViewById(R.id.grid_nameTv);
                holder.grid_item = (PercentLinearLayout) view.findViewById(R.id.grid_item);
                view.setTag(holder);
            }else{
                holder = (ViewHolder) view.getTag();
            }
            Glide.with(context).load(octoberList.get(i).getCover()).into(holder.grid_imageTv);

            holder.grid_nameTv.setText(octoberList.get(i).getTitle());
            holder.grid_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent it = new Intent(context, TomatoDetails.class);
                    context.startActivity(it);
                }
            });
            return view;
        }
        class ViewHolder{
            ImageView grid_imageTv;
            TextView grid_nameTv;
            PercentLinearLayout grid_item;
        }
    }
}
